<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('中介团队') }}</span>
    </div>
    <el-form ref="ZJQueryForm" :model="ZJQueryParams" :inline="true">
      <el-form-item :label="$t('团队名称')" prop="teamName">
        <el-input
          v-model="ZJQueryParams.teamName"
          :placeholder="$t('团队名称')"
          clearable
          @keyup.enter.native="queryHandle('ZJ')"
        />
      </el-form-item>
      <el-form-item :label="$t('姓名')" prop="userName">
        <el-input
          v-model="ZJQueryParams.userName"
          :placeholder="$t('姓名')"
          clearable
          @keyup.enter.native="queryHandle('ZJ')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle('ZJ')">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('ZJ')">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle('ZJ')"
        >{{ $t('新增') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="ZJLoading" :data="ZJList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (ZJQueryParams.pageNum - 1) * ZJQueryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('中介机构')" align="center" prop="mediationName" show-overflow-tooltip />
      <el-table-column :label="$t('团队名称')" align="center" prop="teamName" show-overflow-tooltip />
      <el-table-column :label="$t('姓名')" align="center" prop="userName" show-overflow-tooltip />
      <el-table-column :label="$t('邮箱')" align="center" prop="email" show-overflow-tooltip />
      <el-table-column :label="$t('状态')" align="center" prop="status" width="100px" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="Y"
            inactive-value="N"
            @change="statusChangeHandle(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row, 'ZJ')"
          >{{ $t('修改') }}</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row, 'ZJ')"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="ZJTotal>0"
      :total="ZJTotal"
      :page.sync="ZJQueryParams.pageNum"
      :limit.sync="ZJQueryParams.pageSize"
      @pagination="getList('ZJ')"
    />

    <div class="area-header" style="margin-top: 20px;">
      <span class="area-header-title">{{ $t('被投企业团队') }}</span>
    </div>
    <el-form ref="QTQueryForm" :model="QTQueryParams" :inline="true">
      <el-form-item :label="$t('姓名')" prop="userName">
        <el-input
          v-model="QTQueryParams.userName"
          :placeholder="$t('姓名')"
          clearable
          @keyup.enter.native="queryHandle('QT')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle('QT')">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('QT')">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle('QT')"
        >{{ $t('新增') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="QTLoading" :data="QTList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (QTQueryParams.pageNum - 1) * QTQueryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('姓名')" align="center" prop="userName" show-overflow-tooltip />
      <el-table-column :label="$t('邮箱')" align="center" prop="email" show-overflow-tooltip />
      <el-table-column :label="$t('状态')" align="center" prop="status" width="100px" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="Y"
            inactive-value="N"
            @change="statusChangeHandle(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row, 'QT')"
          >{{ $t('修改') }}</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row, 'QT')"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="QTTotal>0"
      :total="QTTotal"
      :page.sync="QTQueryParams.pageNum"
      :limit.sync="QTQueryParams.pageSize"
      @pagination="getList('QT')"
    />

    <!-- 添加或修改DataRoom外部用户对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" width="30%" :close-on-click-modal="false" :lock-scroll="false" custom-class="" append-to-body>
      <ddExternalUserForm ref="ddExternalUserForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDdExternalUser, delDdExternalUser, exportDdExternalUser, statusChange } from '@/api/dd/user/ddExternalUser'
import ddExternalUserForm from './ddExternalUserForm.vue'
export default {
    components: {
        ddExternalUserForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          ZJLoading: true,
          QTLoading: true,
          // 总条数
          ZJTotal: 0,
          QTTotal: 0,
          // DataRoom外部用户表格数据
          // 中介团队
          ZJList: [],
          // 其他团队
          QTList: [],
          // 弹出层标题
          title: '团队成员',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          ZJQueryParams: {
            type: 'ZJ',
            dealId: this.$route.query.id,
            teamName: undefined,
            userName: undefined,
            email: undefined,
            status: undefined,
            pageNum: 1,
            pageSize: 10
          },
          QTQueryParams: {
            type: 'QT',
            dealId: this.$route.query.id,
            teamName: undefined,
            userName: undefined,
            email: undefined,
            status: undefined,
            pageNum: 1,
            pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
    // 查询中介团队
    this.getList('ZJ')
    // 查询其他团队
    this.getList('QT')
  },
  methods: {
    /** 查询团队成员 */
    getList(type) {
      this[type + 'Loading'] = true
      listDdExternalUser(this[type + 'QueryParams']).then(response => {
        this[type + 'List'] = response.rows
        this[type + 'Total'] = response.total
        this[type + 'Loading'] = false
      }).catch(() => {
        this[type + 'Loading'] = false
      })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: this.$route.query.id,
            teamName: undefined,
            userName: undefined,
            email: undefined,
            status: 'Y',
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle(type) {
      this[type + 'QueryParams'].pageNum = 1
      this.getList(type)
    },
    /** 重置按钮操作 */
    resetQuery(type) {
      this.resetForm(type + 'QueryForm')
      this.queryHandle(type)
    },
    /** 新增按钮操作 */
    addHandle(type) {
        this.formOpen = true
        if (type === 'ZJ') {
          this.title = '中介团队'
        } else {
          this.title = '其他团队'
        }
        this.$nextTick(() => {
            this.$refs.ddExternalUserForm.init(null, type)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row, type) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.ddExternalUserForm.init(row, type)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row, type) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delDdExternalUser(row.id)
        }).then(() => {
            this.getList(type)
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return exportDdExternalUser(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(() => {
        })
    },
    statusChangeHandle(row) {
      statusChange(row.id, row.status).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('修改成功'))
            }
        })
    }
  }
}
</script>
